package cn.kent.simple;

import java.util.*;

/**
 * 155. 最小栈
 * 设计一个支持 push ，pop ，top 操作，
 * 并能在常数时间内检索到最小元素的栈。
 */
class MinStack {

    Deque<Integer> xStack;
    Deque<Integer> minStack;

    public MinStack() {
        xStack = new LinkedList<Integer>();
        minStack = new LinkedList<Integer>();
        minStack.push(Integer.MAX_VALUE);
    }

    public void push(int val) {
        xStack.push(val);
        minStack.push(Math.min(minStack.peek(), val));
    }

    public void pop() {
        xStack.pop();
        minStack.pop();
    }

    public int top() {
        return xStack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    static class Main {
        public static void main(String[] args) {
            MinStack minStack = new MinStack();
            minStack.push(-2);
            minStack.push(0);
            minStack.push(-3);
            int min = minStack.getMin();
            System.out.println(min);
            minStack.pop();
            minStack.top();
            System.out.println(minStack.getMin());
        }
    }
}

